<style lang="less">
</style>

<template>
    <div>
        <Row :gutter="2">
            <Col span="24">
                <Card shadow>
                    <iview-table
                            ref="table"
                            :span1="18"
                            :span2="6"
                            :baseURL="queryModel.baseURL"
                            :menuId="dataModel.menuId"
                            :columns="dataModel.columns"
                            :order="dataModel.order"
                            :pageNumber="pageModel.pageNumber"
                            :pageSize="pageModel.pageSize"
                            v-on="{changeTotalSize:changeTotalSize,events:events,dblclick:dblclick,changeSelect:changeSelect}">
                    </iview-table>
                    <br>
                    <iview-page
                            :totalSize="pageModel.totalSize"
                            :pageNumber="pageModel.pageNumber"
                            :pageSize="pageModel.pageSize"
                            :pageSizeOpts="pageModel.pageSizeOpts"
                            v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
                    </iview-page>
                </Card>
            </Col>
        </Row>
        <Row>
            <Col span="24">
                <Card shadow>
                    <Row>
                        <Col span="24">
                            <user-role-details ref="userRoleDetails" :id="selectRow.id" :user="selectRow"></user-role-details>
                        </Col>
                    </Row>
                    <Row>
                        <Col span="24">
                            <user-organ-details ref="userOrganDetails"></user-organ-details>
                        </Col>
                    </Row>
                </Card>
            </Col>
        </Row>
    </div>
</template>

<script>
import iviewTable from '@/view/common/table/Table.vue';
import iviewPage from '@/view/common/page/Page.vue';
import userRoleDetails from '@/view/system/user/UserRoleDetails.vue';
import userOrganDetails from '@/view/system/user/UserOrganDetails.vue';
export default {
    name: 'text-editor',
    components: {
        iviewTable,
        iviewPage,
        userRoleDetails,
        userOrganDetails
    },
    data () {
        return {
            baseURL: '/user',
            selectRow: {},
            selectRows: [],
            queryModel: {
                // 网关代理路径(必须)+资源路径(可选)
                baseURL: '/user'
            },
            dataModel: {
                menuId: 110,
                columns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },{
                        title: '登录用户名',
                        key: 'userLoginName',
                        sortable: true
                    },{
                        title: '用户名',
                        key: 'username',
                        sortable: true
                    },{
                        title: '职务',
                        key: 'job',
                        sortable: true,
                        render: (h, params) => {
                            let temp = params.row.job
                            if (temp === 1) {
                                temp = '医生'
                            } else if (temp === 2) {
                                temp = '护士'
                            } else if (temp === 3) {
                                temp = '网店'
                            } else if (temp === 4) {
                                temp = '客服'
                            } else if (temp === 5) {
                                temp = '现场'
                            } else if (temp === 6) {
                                temp = '财务'
                            } else if (temp === 7) {
                                temp = '前台'
                            } else if (temp === 8) {
                                temp = '后勤'
                            } else if (temp === 9) {
                                temp = '主管'
                            } else if (temp === 10) {
                                temp = '总经理'
                            }
                            return h('span', temp)
                        }
                    }, {
                        title: '部门',
                        key: 'departmentName'
                    },{
                        title: '后勤',
                        key: 'isLogistics',
                        sortable: true,
                        render: (h, params) => {
                            let temp = params.row.isLogistics
                            if (temp === 0) {
                                temp = '否'
                            } else if (temp === 1) {
                                temp = '是'
                            }
                            return h('span', temp)
                        }
                    },{
                        title: '专家',
                        key: 'isExpert',
                        sortable: true,
                        render: (h, params) => {
                            let temp = params.row.isExpert
                            if (temp === 0) {
                                temp = '否'
                            } else if (temp === 1) {
                                temp = '是'
                            }
                            return h('span', temp)
                        }
                    },{
                        title: '状态',
                        key: 'status',
                        sortable: true,
                        render: (h, params) => {
                            let color = ''
                            let temp = params.row.status
                            if (temp === 0) {
                                temp = '激活'
                                color = '#19be6b'
                            } else if (temp === 1) {
                                temp = '冻结'
                                color = '#ed3f14'
                            }
                            return h('Tag',{
                                props: {
                                    type: 'dot',
                                    color: color
                                }
                            }, temp)
                        }
                    },{
                        title: '在线状态',
                        // key: 'onlineStatus',
                        sortable: true,
                        render: (h, params) => {
                            let color = ''
                            let temp = params.row.onlineStatus
                            if (temp === 1) {
                                temp = '在线'
                                color = '#19be6b'
                            } else if (temp === 0) {
                                temp = '离线'
                                color = '#80848f'
                            }
                            return h('Tag',{
                                props: {
                                    type: 'dot',
                                    color: color
                                }
                            }, temp)
                        }
                    },
                    {
                        title: '操作',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'default'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.offline(params.row)
                                        }
                                    }
                                }, '强制下线')
                            ]);
                        }
                    }
                ],
                order: 'id desc'
            },
            pageModel: {
                totalSize: 0,
                pageNumber: 1,
                pageSize: 10,
                pageSizeOpts: [10, 50, 100, 500]
            }
        };
    },
    methods: {
        events (item) {
            let path = item.path
            if (path === 'add') {
                this.add()
            } else if (path === 'edit') {
                this.edit()
            } else if (path === 'addRole') {
                this.addRole()
            } else if (path === 'addOrgan') {
                this.addOrgan()
            } else if (path === 'freeze') {
                this.freeze()
            } else if (path === 'use') {
                this.use()
            } else if (path === 'delete') {
                this.delete()
            }
        },
        add () {
            this.$bus.toPath('/user/details')
        },
        edit () {
            if (this.selectRows.length === 1) {
                let row = this.selectRows[0]
                let query = {
                    row: row
                }
                this.$bus.toPath('/user/details', query)
            } else {
                this.$bus.alert('warning', '请选择一条记录')
            }
        },
        addRole () {
            if (this.selectRows.length !== 1) {
                this.$bus.alert('warning', '请选择一条记录', 3)
                this.status = !this.status
                return
            }
            let row = this.selectRows[0]
            let query = {
                id: row.id,
                user: row
            }
            this.$bus.toPath('/user/roleDetails', query)
        },
        addOrgan () {
            if (this.selectRows.length !== 1) {
                this.$bus.alert('warning', '请选择一条记录', 3)
                this.status = !this.status
                return
            }
            let row = this.selectRows[0]
            let query = {
                id: row.id,
                user: row
            }
            this.$bus.toPath('/user/organDetails', query)
        },
        freeze () {
            if (this.selectRows.length > 0) {
                let ids = []
                for (let row of this.selectRows) {
                    ids.push(row.id)
                }
                this.$Modal.confirm({
                    title: '操作提示',
                    content: '<p>确认冻结'+ids.length+'条记录?</p>',
                    onOk: () => {
                        this.doFreeze(ids)
                    }
                })
            } else {
                this.$bus.alert('warning', '请至少选择一条记录')
            }
        },
        doFreeze (ids) {
            let url = this.baseURL + '/freeze'
            this.$ajax.post(url, ids).then(r => {
                let {code, msg} = r.data
                if (code === 0) {
                    this.$bus.alert('success', '已冻结')
                    this.refresh()
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        use () {
            if (this.selectRows.length > 0) {
                let ids = []
                for (let row of this.selectRows) {
                    ids.push(row.id)
                }
                this.$Modal.confirm({
                    title: '操作提示',
                    content: '<p>确认激活'+ids.length+'条记录?</p>',
                    onOk: () => {
                        this.doUse(ids)
                    }
                })
            } else {
                this.$bus.alert('warning', '请至少选择一条记录')
            }
        },
        doUse (ids) {
            let url = this.baseURL + '/use'
            this.$ajax.post(url, ids).then(r => {
                let {code, msg} = r.data
                if (code === 0) {
                    this.$bus.alert('success', '已激活')
                    this.refresh()
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        delete () {
            if (this.selectRows.length > 0) {
                let ids = []
                for (let row of this.selectRows) {
                    ids.push(row.id)
                }
                this.$Modal.confirm({
                    title: '操作提示',
                    content: '<p>确认删除'+ids.length+'条记录?</p>',
                    onOk: () => {
                        this.doDelete(ids)
                    }
                })
            } else {
                this.$bus.alert('warning', '请至少选择一条记录')
            }
        },
        doDelete (ids) {
            let url = this.baseURL + '/delete'
            this.$ajax.post(url, ids).then(r => {
                let {code, msg} = r.data
                if (code === 0) {
                    this.$bus.alert('success', '已删除')
                    this.refresh()
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        dblclick (row, index) {
            this.$refs.userRoleDetails.id = row.id
            this.$refs.userRoleDetails.user = row
            this.$refs.userRoleDetails.init()
            this.$refs.userOrganDetails.id = row.id
            this.$refs.userOrganDetails.user = row
            this.$refs.userOrganDetails.init()
            this.selectRow = row
        },
        offline (row) {
            let clientId = this.$store.state.app.loginUser.id
            if (row.id !== clientId) {
                if (row.onlineStatus === 1) {
                    this.$Modal.confirm({
                        title: '操作提示',
                        content: '<p>确认操作?</p>',
                        onOk: () => {
                            this.doOffline(row)
                        }
                    })
                } else {
                    this.$bus.modal('warning', '系统提示', '对方已离线')
                }
            } else {
                this.$bus.modal('warning', '系统提示', '你不可以强制自己下线')
            }
        },
        doOffline (row) {
            let url = '/user/socket/send'
            this.$ajax1.post(url, {
                clientId: row.id
            }).then(r => {
                let {code, msg} = r.data
                if (code === 0) {
                    this.$bus.alert('success', '操作成功')
                    this.refresh()
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        changeSelect (rows) {
            this.selectRows = rows
        },
        changeTotalSize (totalSize) {
            this.pageModel.totalSize = totalSize
        },
        changePageNumber (pageNumber) {
            this.pageModel.pageNumber = pageNumber
        },
        changePageSize (pageSize) {
            this.pageModel.pageSize = pageSize
        },
        refresh () {
            this.$refs.table.refresh()
        },
        init () {
            this.refresh()
        }
    },
    mounted () {
        this.init()
    },
    watch: {
        '$route' (to) {
            if (to.path === '/user') {
                this.init()
            }
        }
    }
};
</script>

<style>

</style>
